package com.example.xutilsdemo.db

import android.database.Cursor
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import org.xutils.db.converter.ColumnConverter
import org.xutils.db.sqlite.ColumnDbType

/**
 *@author: njb
 *@date:  2023/5/27 18:41
 *@desc:
 */
class ListDevicesConverter :ColumnConverter<ListDevices<DevicesInfoBean>>{
    override fun getFieldValue(
        cursor: Cursor,
        index: Int
    ): ListDevices<DevicesInfoBean>? {
        return if(cursor.isNull(index)){
            null
        }else {
            val s = cursor.getString(index)
            val gson = Gson()
            gson.fromJson(s, object : TypeToken<ListDevices<DevicesInfoBean>>() {}.type
            )
        }
    }

    override fun getColumnDbType(): ColumnDbType {
        return ColumnDbType.TEXT
    }

    override fun fieldValue2DbValue(fieldValue: ListDevices<DevicesInfoBean>?): Any {
       val gson = Gson()
        return gson.toJson(fieldValue)
    }
}